package com.qianfeng.admin.controller;

import com.qianfeng.entity.dto.Merchant;
import com.qianfeng.entity.vo.MyPage;
import com.qianfeng.http.Result;
import com.qianfeng.service.impl.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("merchant")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;


    @RequestMapping("list")
    public String toList(Model model , @RequestParam(value = "pageNum" , required = false , defaultValue = "1") Long pageNum){
        MyPage<Merchant> myPage = merchantService.listPage(pageNum);
        model.addAttribute("merchantList" , myPage.getObjList());
        model.addAttribute("pageList" , myPage.getPageList());
        model.addAttribute("pageNum" , pageNum);
        return "merchant/list";
    }

    @RequestMapping("getById")
    @ResponseBody
    public Result<?> getById(@RequestParam(value = "id" , required = true) Long merchantId){
        return Result.success(merchantService.getById(merchantId));
    }

    @RequestMapping("save")
    @ResponseBody
    public Result<?> save(Merchant merchant){
        merchantService.saveOrUpdate(merchant);
        return Result.success();
    }

    @RequestMapping("del")
    @ResponseBody
    public Result<?> del(@RequestParam(value = "id" , required = true) Long merchantId){
        merchantService.removeById(merchantId);
        return Result.success();
    }

}
